<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>书籍分享平台-管理员</h2>
      </div>

      <!-- 登录表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        size="large"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username"
            placeholder="用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <div class="form-actions">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        </div>

        <div class="button-group">
          <el-button 
            type="primary" 
            class="submit-btn" 
            @click="handleLogin"
          >
            登录
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAdminStore } from '@/stores/admin'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/admin'

const router = useRouter()
const adminStore = useAdminStore()
const rememberMe = ref(false)

// 登录表单
const loginFormRef = ref(null)
const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9]{3,10}$/, message: '用户名只能包含3-10位字母或数字', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9]{6,10}$/, message: '密码只能包含6-10位字母或数字', trigger: 'blur' }
  ]
}

// 登录处理
const handleLogin = async () => {
  try {
    // 表单验证
    const valid = await loginFormRef.value.validate()
      .catch(() => {
        ElMessage.warning('请填写完整的登录信息')
        return false
      })
    
    if (!valid) return

    // 调用登录接口
    const response = await login({
      username: loginForm.username,
      password: loginForm.password,
      remember: rememberMe.value
    })
    
    if (response.code === 200) {
      // 保存登录状态
      const { token, admin } = response.data
      adminStore.setToken(token)
      adminStore.setAdminInfo(admin)
      
      // 提示并跳转
      ElMessage.success('登录成功')
      router.push('/admin')
    } else {
      ElMessage.error(response.message || '登录失败')
    }
  } catch (error) {
    console.error('Login error:', error)
    ElMessage.error(error.response?.data?.message || '登录失败')
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.login-box {
  width: 400px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  padding: 35px 40px;
}

.login-header {
  text-align: center;
  margin-bottom: 35px;
}

.login-header h2 {
  color: #409EFF;
  margin-bottom: 25px;
  font-size: 24px;
  font-weight: 600;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
  width: 280px;
  margin-left: auto;
  margin-right: auto;
}

:deep(.el-input__wrapper) {
  padding: 1px 15px;
  height: 42px;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #c0c4cc;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409EFF;
}

.form-actions {
  width: 280px;
  margin: 0 auto 25px;
}

.button-group {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.submit-btn {
  width: 180px;
  height: 42px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 21px;
}
</style>
